﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 工作组 人员DAL
    /// </summary>
    public class HRWorkGrpMember
    {
        /// <summary>
        ///  是否存在该记录
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构代码</param>
        /// <param name="fWorkGrpNo">工作组编号</param>
        /// <param name="fEmplNo">人员编号</param>
        /// <returns>是否存在</returns>
        public bool Exists(string fEntityNo, string fWorkGrpNo, string fEmplNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_HRWorkGrpMember");
            strSql.Append(" where FEntityNo=@FEntityNo and FWorkGrpNo=@FWorkGrpNo and FEmplNo=@FEmplNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fWorkGrpNo;
            parameters[2].Value = fEmplNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 得到一个实体对象
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fWorkGrpNo">工作组编号</param>
        /// <param name="fEmplNo">人员编号</param>
        /// <returns>工作组人员信息</returns>
        public Model.HRWorkGrpMember GetModel(string fEntityNo, string fWorkGrpNo, string fEmplNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FEntityNo,FWorkGrpNo,FEmplNo,FWorkGrpDutyNo,FWorkDuty,FSeq from t_HRWorkGrpMember ");
            strSql.Append(" where FEntityNo=@FEntityNo and FWorkGrpNo=@FWorkGrpNo and FEmplNo=@FEmplNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fWorkGrpNo;
            parameters[2].Value = fEmplNo;

            Model.HRWorkGrpMember model = new Model.HRWorkGrpMember();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                model.FWorkGrpNo = ds.Tables[0].Rows[0]["FWorkGrpNo"].ToString();
                model.FEmplNo = ds.Tables[0].Rows[0]["FEmplNo"].ToString();
                model.FWorkGrpDutyNo = ds.Tables[0].Rows[0]["FWorkGrpDutyNo"].ToString();
                model.FWorkDuty = ds.Tables[0].Rows[0]["FWorkDuty"].ToString();
                if (ds.Tables[0].Rows[0]["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(ds.Tables[0].Rows[0]["FSeq"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 批量增加工作组职务人员（事务）
        /// </summary>
        /// <param name="members">需增加的工作组人员列表</param>
        public void Add(List<Model.HRWorkGrpMember> members)
        {
            if (members != null && members.Count > 0)
            {
                List<CommandInfo> cmdList = new List<CommandInfo>();
                CommandInfo cmds = null;
                StringBuilder strSql = null;
                string fEntityNo = string.Empty;
                foreach (Model.HRWorkGrpMember model in members)
                {
                    cmds = new CommandInfo();
                    strSql = new StringBuilder();
                    strSql.Append("insert into t_HRWorkGrpMember(");
                    strSql.Append("FEntityNo,FWorkGrpNo,FEmplNo,FWorkGrpDutyNo,FWorkDuty,FSeq)");
                    strSql.Append(" values (");
                    strSql.Append("@FEntityNo,@FWorkGrpNo,@FEmplNo,@FWorkGrpDutyNo,@FWorkDuty,@FSeq)");
                    SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FWorkGrpDutyNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FWorkDuty", SqlDbType.Text),
					new SqlParameter("@FSeq", SqlDbType.Int,4)};
                    parameters[0].Value = model.FEntityNo;
                    parameters[1].Value = model.FWorkGrpNo;
                    parameters[2].Value = model.FEmplNo;
                    parameters[3].Value = model.FWorkGrpDutyNo;
                    parameters[4].Value = model.FWorkDuty;
                    parameters[5].Value = model.FSeq;

                    fEntityNo = model.FEntityNo;
                    cmds.CommandText = strSql.ToString();
                    cmds.Parameters = parameters;
                    cmdList.Add(cmds);
                }

                //执行存储过程，生成临时表数据，以便查询时调用 
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("EXEC sp_genDocAuthorityDW @EntityNo");
                SqlParameter[] parameters2 = {
					new SqlParameter("@EntityNo", SqlDbType.NVarChar,12)};
                parameters2[0].Value = fEntityNo;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);

                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }

        }

        /// <summary>
        /// 更新一条数据
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="model">工作组人员信息</param>
        public void Update(Model.HRWorkGrpMember model)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_HRWorkGrpMember set ");
            strSql.Append("FWorkGrpDutyNo=@FWorkGrpDutyNo,");
            strSql.Append("FWorkDuty=@FWorkDuty,");
            strSql.Append("FSeq=@FSeq");
            strSql.Append(" where FEntityNo=@FEntityNo and FWorkGrpNo=@FWorkGrpNo and FEmplNo=@FEmplNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FWorkGrpDutyNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FWorkDuty", SqlDbType.Text),
					new SqlParameter("@FSeq", SqlDbType.Int,4)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FWorkGrpNo;
            parameters[2].Value = model.FEmplNo;
            parameters[3].Value = model.FWorkGrpDutyNo;
            parameters[4].Value = model.FWorkDuty;
            parameters[5].Value = model.FSeq;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //执行存储过程，生成临时表数据，以便查询时调用 
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("EXEC sp_genDocAuthorityDW @EntityNo");
            SqlParameter[] parameters2 = {
					new SqlParameter("@EntityNo", SqlDbType.NVarChar,12)};
            parameters2[0].Value = model.FEntityNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }

        /// <summary>
        /// 删除一条数据
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fWorkGrpNo">工作组编号</param>
        /// <param name="fEmplNo">人员编号</param>
        public void Delete(string fEntityNo, string fWorkGrpNo, string fEmplNo)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_HRWorkGrpMember ");
            strSql.Append(" where FEntityNo=@FEntityNo and FWorkGrpNo=@FWorkGrpNo and FEmplNo=@FEmplNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FWorkGrpNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = fEntityNo;
            parameters[1].Value = fWorkGrpNo;
            parameters[2].Value = fEmplNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //执行存储过程，生成临时表数据，以便查询时调用 
            strSql = new StringBuilder();
            cmds = new CommandInfo();
            strSql.Append("EXEC sp_genDocAuthorityDW @EntityNo");
            SqlParameter[] parameters2 = {
					new SqlParameter("@EntityNo", SqlDbType.NVarChar,12)};
            parameters2[0].Value = fEntityNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }
        /// <summary>
        /// 根据公司编号从视图中获取工作组人员信息
        ///<creator>wangl</creator>
        /// <date>2013-08-10</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <returns>DataSet对象</returns>
        public DataSet GetWorkGrpEmplNoSByView(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from v_HRWorkGrpMember ");
            strSql.Append(" where FEntityNo=@FEntityNo");
            strSql.Append(" order by Fseq");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = entityNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
    }
}
